package com.finance.action.android;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.vo.CommonConfig;
import com.common.vo.JsonVo;
import com.finance.entity.Balance;
import com.finance.entity.User;
import com.finance.service.BalanceService;
import com.finance.service.UserService;
import com.web.utils.UserManagerUtil;

@Controller("UserAction")
public class UserAction {
	
	@Resource(name = "UserService")
	private UserService userService;
	
	@Resource(name = "BalanceService")
	private BalanceService balanceService;
	/**
	 * 分配账号（注册）
	 * @param name 姓名
	 * @param phone 电话
	 * @param password 密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("reg")
	public JsonVo<String> addYhUser_ajax(String name,String phone,String password){
		JsonVo<String> json = new JsonVo<String>();
		User User1 = userService.findByPhone(phone,1);
		if (User1 != null) {
			json.setCode(1);
			json.setMsg("该手机号码已注册过");
			json.setSuccess(false);
		}else {
			User user = new User();
			user.setUserId(CommonConfig.createUserId());
			user.setPassword(password);
			user.setPhone(phone);
			user.setState(1);
			user.setName(name);
			user.setCreationTime(new Date());
			user.setJurisdiction(1);
			userService.insert(user);
			
			Balance balance = new Balance();
			balance.setUserId(user.getUserId());
			balance.setBalanceCount((long) 0);
			json.setCode(200);
			json.setMsg("成功");
		}
		return json;
	}
	/**
	 * 登陆
	 * @param phone 电话
	 * @param password 密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("login")
	public JsonVo<User> loginCheck(String phone, String password) {
		String userName = new String(phone);
		String Pd = new String(password);
		JsonVo<User> json = new JsonVo<User>();
		try {
			// 1.用户名不能为空
			if (StringUtils.isEmpty(userName)) {
				json.setSuccess(false);
				json.setMsg("用户名不能为空");
				return json;
			}
			// 2.密码不能为空
			if (StringUtils.isEmpty(Pd)) {
				json.setSuccess(false);
				json.setMsg("密码不能为空");
				return json;
			}
			UsernamePasswordToken token = new UsernamePasswordToken(userName,
					Pd);
			token.setRememberMe(true);
			Subject currentUser = SecurityUtils.getSubject();

			currentUser.login(token);
			if (!currentUser.isAuthenticated()) {
				json.setSuccess(false);
				json.setMsg("请输入正确的用户名或密码");
				return json;
			}
			User user = UserManagerUtil.getCurrentUser();
			json.setContent(user);
			json.setSuccess(true);
			json.setCode(200);
			json.setMsg("登陆成功");
			return json;
		} catch (IncorrectCredentialsException ice) {
			json.setSuccess(false);
			json.setMsg("请输入正确的用户名或密码");
			return json;
		} catch (AccountException e) {
			json.setSuccess(false);
			json.setMsg("请输入正确的用户名或密码");
			return json;
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("请输入正确的用户名或密码");
			return json;
		}

	}

}
